<?php

/**
 * @author HungPV <phamvanhung0818@gmail.com>
 * @todo managment members controller
 */
class members extends MX_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('member');
    }

    function index() {
        $this->user->check_permission();
        // get list members
        $members = $this->member->get();
        $data['members'] = $members;

        $data['view'] = 'members_index';
        $this->load->view('template', $data);
    }

    function add() {
        $this->user->check_permission();
        if ($this->input->post('name')) {
            $name = $this->input->post('name');
            $description = $this->input->post('description');
            $position = $this->input->post('position');
            $image = $this->input->post('hdffeatured_image');
            $facebook = $this->input->post('facebook');
            $skype = $this->input->post('skype');
            $phone = $this->input->post('phone');
            $email = $this->input->post('email');

            if ($this->member->add($name, $position, $description, $image, $facebook, $skype, $phone, $email)) {
                $this->session->set_flashdata('insert_member_success', true);
            } else {
                $this->session->set_flashdata('insert_member_error', true);
            }
            redirect('administrator/members/add');
        }

        $data['view'] = 'members_add';
        $this->load->view('template', $data);
    }

    function edit($id = 0) {
        $this->user->check_permission();
        if ($this->input->post('name')) {
            $id = $this->input->post('id');
            $name = $this->input->post('name');
            $description = $this->input->post('description');
            $position = $this->input->post('position');
            $image = $this->input->post('hdffeatured_image');
            $facebook = $this->input->post('facebook');
            $skype = $this->input->post('skype');
            $phone = $this->input->post('phone');
            $email = $this->input->post('email');

            if ($this->member->edit($id, $name, $position, $description, $image, $facebook, $skype, $phone, $email)) {
                $this->session->set_flashdata('update_member_success', true);
            } else {
                $this->session->set_flashdata('update_member_error', true);
            }
            redirect('administrator/members/edit/' . $id);
        }

        // get current member
        $member = $this->member->get($id);
        if ($member != NULL) {
            $data['member'] = $member;
        }        

        $data['view'] = 'members_update';
        $this->load->view('template', $data);
    }

    function delete() {
        $this->user->check_permission();
        $id = $this->input->post('id');
        if ($this->member->delete($id)) {
            echo json_encode(array('result' => TRUE));
        } else {
            echo json_encode(array('result' => FALSE));
        }
    }

}

?>
